home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / csim / source.lha / source / Threads / GnuThreads / sig.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-14  |  2.3 KB  |  106 lines

  1. /*
  2.  * sig.c -- share signals among threads.
  3.  *
  4.  * This is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 1, or (at your option)
  7.  * any later version.
  8.  *
  9.  * This software is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * see the file COPYING.  If not, write to
  16.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  *
  18.  * author: Stephen Crane, (jsc@doc.ic.ac.uk), Department of Computing,
  19.  * Imperial College of Science, Technology and Medicine, 180 Queen's
  20.  * Gate, London SW7 2BZ, England.
  21.  */
  22.  
  23. #include <sysent.h>
  24. #include <malloc.h>
  25. #include <sys/types.h>
  26. #include <sys/time.h>
  27. #include <signal.h>
  28. #include "gnulwp.h"
  29.  
  30. static struct siginfo *info;
  31. static int w, n;
  32. static fd_set fds;
  33. static struct sigvec osv;
  34.  
  35. /*
  36.  * iohan -- main handler for sigio.  dispatches signal to
  37.  * first ready descriptor.
  38.  */
  39. static void iohan ()
  40. {
  41.     struct fd_set fdtmp;
  42.     struct timeval t;
  43.     struct siginfo *p;
  44.     int pri;
  45.     extern int maxpri;
  46.  
  47.     fdtmp = fds;
  48.     t.tv_sec = t.tv_usec = 0;
  49.     select (w, &fdtmp, (fd_set *)0, (fd_set *)0, &t);
  50.     for (p=info; p; p=p->next)
  51.         if (FD_ISSET(p->des, &fdtmp)) {
  52.             p->han (p->ctx, p->des);
  53.             break;
  54.         }
  55. }
  56.  
  57. /*
  58.  * sigioset -- install handler for SIGIO
  59.  */
  60. int sigioset (int fd, void (*handler) (void*, int), void *context)
  61. {
  62.     struct siginfo *p;
  63.     struct sigvec sv;
  64.  
  65.     if (!n) {
  66.         w = getdtablesize ();
  67.         FD_ZERO(&fds);
  68.         sv.sv_handler = iohan;
  69.         sv.sv_mask = 0;
  70.         sv.sv_flags = SV_INTERRUPT;
  71.         sigvec (SIGIO, &sv, &osv);
  72.     }
  73.     if (n++ == w)
  74.         return (-1);
  75.     p = (struct siginfo *)malloc (sizeof(struct siginfo));
  76.     p->next = info;
  77.     p->han = handler;
  78.     p->des = fd;
  79.     p->ctx = context;
  80.     info = p;
  81.     FD_SET(fd, &fds);
  82.     return (0);
  83. }
  84.  
  85. /*
  86.  * sigioclr -- remove handler for SIGIO
  87.  */
  88. int sigioclr (int fd)
  89. {
  90.     struct siginfo *p, *q;
  91.  
  92.     for (p=info, q=0; p; q=p, p=p->next)
  93.         if (p->des == fd)
  94.             break;
  95.     if (!p) return (-1);
  96.  
  97.     if (q) q->next = p->next;
  98.     else info = p->next;
  99.     FD_CLR(fd, &fds);
  100.  
  101.     free (p);
  102.     if (!--n)
  103.         sigvec (SIGIO, &osv, (struct sigvec *)0);
  104.     return (0);
  105. }
  106.